#include <stdio.h>
#include <stdlib.h>

typedef short int bool;
#define true    1
#define false   0

#define M   30
const int MAXINT = 101;

int n = 0;
int roadsmap[M][M];

void init()
{
    int i = 0, j = 0;
    for(i = 0; i < M; i++)
    {
        for(j = 0; j < M; j++)
        {
            roadsmap[i][j] = MAXINT;
        }
    }
}

int getindex(char vertex)
{
    return vertex - 'A';
}


bool findnextv(short int s[M], int* nextv, int* cost)
{
    int i = 0, j = 0;

    *cost = MAXINT;
    *nextv = -1;

    for(i = 0; i < n; i++)
    {
        if(s[i] == 0)
        {
            continue;
        }
        for(j = 0; j < n; j++)
        {
            if(s[j] == 1)
            {
                continue;
            }
            if(roadsmap[i][j] < *cost)
            {
                *cost = roadsmap[i][j];
                *nextv = j;
            }
        }
    }
    return ((*nextv) == -1) ? false : true;
}

void prim(int* mincost)
{
    short int s[M];
    int i = 0, nextv = -1, cost = 0;

    for(i = 0; i < n; i++)
    {
        s[i] = 0;
    }
    s[0] = 1;
    *mincost = 0;
    while(1)
    {
        if(findnextv(s, &nextv, &cost) == false)
        {
            break;
        }
        s[nextv] = 1;
        *mincost += cost;

    }
}
//Prim演算法與Kruskal
void process()
{
    int mincost = 0;

    prim(&mincost);

    printf("%d\n", mincost);
}

void getinput()
{
    char vstr[2] = "", vnstr[2] = "";
    int v, vn;
    int  du = 0, cost = 0;
    int i = 0, j = 0;
    while(1)
    {
        init();
        scanf("%d", &n);
        if(n == 0)
        {
            break;
        }
        for(i = 0; i < n-1; i++)
        {
            scanf("%s %d", vstr, &du);
            for(j = 0; j < du; j++)
            {
                scanf("%s %d", vnstr, &cost);
                v = getindex(vstr[0]);
                vn = getindex(vnstr[0]);
                roadsmap[v][vn] = cost;
                roadsmap[vn][v] = cost;
            }
        }
        process();
    }
}

int main()
{
    //freopen("bin\\debug\\in.txt", "r", stdin);

    getinput();

    return 0;
}
